package com.lhwl.it.yh.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.lhwl.it.util.RequestMap;
import com.lhwl.it.yh.service.YhService;

@Controller
public class LoginAction {
	private Log log = LogFactory.getLog(this.getClass());
	@Resource
	private YhService yhService;
	
	@RequestMapping(value="/login")
	public String login(HttpServletRequest request,HttpServletResponse response) throws Exception{
		HashMap map=new RequestMap(request);
		List list=this.yhService.list("YhMapper.loginYh", map);
		if(list.size()==1){
			HashMap user=(HashMap) list.get(0);
			request.getSession().setAttribute("yh", user);
			response.getWriter().print("success");
		}else{
			response.getWriter().print("error");
		}
		return null;
	}
	
	@RequestMapping(value="/logout")
	public String logout(HttpServletRequest request,HttpServletResponse response) throws Exception{
		HttpSession session=request.getSession();
		session.removeAttribute("yh");
		session.invalidate();
		return "index";
	}
	
	@RequestMapping(value="/itmanager",method=RequestMethod.GET)
	public String index(HttpServletRequest request,HttpServletResponse response) throws Exception{
		List ls=new ArrayList();
		
		HashMap sessionMap=(HashMap) request.getSession().getAttribute("yh");
		//获得一级菜单
		sessionMap.put("fjd", "0");
		List list=this.yhService.list("YhMapper.getYhCd", sessionMap); 
		
		if(list!=null){
			for(int i=0;i<list.size();i++){
				HashMap fm=(HashMap) list.get(i);
				sessionMap.put("fjd", fm.get("qxid"));
				//根据fjd获得其二级菜单
				List fl=this.yhService.list("YhMapper.getYhCd", sessionMap);
				if(fl!=null){
					//循环查询二级菜单
					List l=new ArrayList();
					for(int j=0;j<fl.size();j++){
						HashMap m=new HashMap();
						HashMap sm=(HashMap) fl.get(j);
						StringBuffer sb=new StringBuffer();
						m.put("text", sm.get("mc"));
						m.put("url", sm.get("url"));
						sessionMap.put("qxid",sm.get("qxid"));
						List zst=this.yhService.list("YhMapper.getYhQxGn", sessionMap);
						if(zst!=null){
							for(int z=0;z<zst.size();z++){
								HashMap zm=(HashMap) zst.get(z);
								sb.append(zm.get("gnid")+":");
							}
							m.put("enable", sb);
						}
						l.add(m);
					}
					fm.put("second",l);
				}
				ls.add(fm);
			}
		}
		System.out.println(ls);
		
		request.setAttribute("list", ls);
		return "/WEB-INF/jsp/main";
	}
	
}
